home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / Networking / TCP / TCPReceive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  1.8 KB  |  70 lines  |  [TEXT/MPS ]

  1. /*
  2. ** James "im" Beninghaus
  3. */
  4.  
  5.  
  6. #include    <Errors.h>
  7. #include    <Devices.h>
  8. #include    <QuickDraw.h>
  9. #include    <stdio.h>
  10.  
  11. #include    <MacTCPCommonTypes.h>>
  12. #include    <AddressXlation.h>
  13. #include    <GetMyIPAddr.h>
  14. #include    <TCPPB.h>
  15. #include    <UDPPB.h>
  16.  
  17. #define        _STORAGE_    true
  18.  
  19. #include    <TCP.h>
  20.  
  21.  
  22. main (int argc, char *argv[], char *envp[]) {
  23.     
  24.     auto    OSErr            osErr            = noErr;
  25.     auto    short            index;
  26.     auto    char            *option;
  27.     auto    char            *parameter;
  28.     auto    TCPNotifyProc    asrProc            = nil;
  29.     auto    TCPiopb            pb;
  30.     auto    StreamPtr        stream;
  31.     auto    char            streamBuf[4096];
  32.     auto    long            streamBufLen    = sizeof(streamBuf);
  33.     auto    Ptr                streamBufPtr;
  34.     auto    char            ioBuf[256]        = "";
  35.     auto    unsigned short    ioBufLen        = sizeof(ioBuf);
  36.     auto    ip_addr            localIP            = cAnyIP;
  37.     auto    ip_port            localPort        = cReceivePort;
  38.     auto    ip_addr            remoteIP        = cAnyIP;
  39.     auto    ip_port            remotePort        = cAnyPort;
  40.     
  41.     InitGraf((Ptr) &qd.thePort);
  42.  
  43.     if (argc > 1) { 
  44.         if ( ('-' != argv[1][0]) && ('n' != argv[1][1]) ) {
  45.             printf("TCPReceive [-n]");
  46.             exit(paramErr);
  47.         } else {
  48.             asrProc = ASR;
  49.         }            
  50.     }
  51.     
  52.     osErr = _TCPInit();
  53.     if (noErr == osErr) {
  54.         osErr = _TCPCreate(&pb, &stream, streamBuf, streamBufLen, (TCPNotifyProc) asrProc, (Ptr) nil,  (TCPIOCompletionProc) nil, false);
  55.         if (noErr == osErr) {
  56.             osErr = _TCPPassiveOpen(&pb, stream, &remoteIP, &remotePort, &localIP, &localPort, (Ptr) nil, (TCPIOCompletionProc) nil, false);
  57.             if (noErr == osErr) {
  58.                 /* receive data from the remote host */
  59.                 osErr = _TCPRcv(&pb, stream, ioBuf, &ioBufLen, (Ptr) nil, (TCPIOCompletionProc) nil, false);
  60.                 if (noErr == osErr)
  61.                     printf("%s\n", ioBuf);
  62.                 osErr = _TCPClose(&pb, stream, nil, (TCPIOCompletionProc) nil, false);
  63.             }
  64.             osErr = _TCPRelease(&pb, stream, &streamBufPtr, &streamBufLen, (TCPIOCompletionProc) nil, false);
  65.         }
  66.     }
  67.     exit(osErr);
  68. }
  69.  
  70.